/**
 * Created by Administrator on 2017/1/13 0013.
 */

var pageSize = 7;//每页条数
var pageStart = 1;//当前页码
var pageCount = 10;//总页数
var islogin = 1;
var type = 0;
$(function () {
    init();

    $(".left-nav-item").eq(0).click(function () {
        window.location.href = "card_pc.html";
    });
    $(".left-nav-item").eq(1).click(function () {
        window.location.href = "apply.record_pc_student.html";
    });
    $(".exit-btn").click(function () {
        $.ajax({
            url: "/huaqiao/StudentFront/exit?" + new Date().getTime(),
            async: false,
            success: function (){
                window.location.href = "login_pc_student.html";
            }
        });
    });
    laydate({
        elem: '#date-out-start',
        format: 'YYYY-MM-DD',
        istime: false,
        isclear: false, //是否显示清空
        istoday: false, //是否显示今天
        festival: true, //显示节日
        choose: function (datas) { //选择日期完毕的回调
            getCardRecords();
        }
    });
    laydate({
        elem: '#date-out-end',
        format: 'YYYY-MM-DD',
        istime: false,
        isclear: false, //是否显示清空
        istoday: false, //是否显示今天
        festival: true, //显示节日
        choose: function (datas) { //选择日期完毕的回调
            getCardRecords();
        }
    });
    laydate({
        elem: '#date-in-start',
        format: 'YYYY-MM-DD',
        istime: false,
        isclear: false, //是否显示清空
        istoday: false, //是否显示今天
        festival: true, //显示节日
        choose: function (datas) { //选择日期完毕的回调
            getCardRecords();
        }
    });
    laydate({
        elem: '#date-in-end',
        format: 'YYYY-MM-DD',
        istime: false,
        isclear: false, //是否显示清空
        istoday: false, //是否显示今天
        festival: true, //显示节日
        choose: function (datas) { //选择日期完毕的回调
            getCardRecords();
        }
    });
    laydate({
        elem: '#date-unusual-start',
        format: 'YYYY-MM-DD',
        istime: false,
        isclear: false, //是否显示清空
        istoday: false, //是否显示今天
        festival: true, //显示节日
        choose: function (datas) { //选择日期完毕的回调
            getCardRecords();
        }
    });
    laydate({
        elem: '#date-unusual-end',
        format: 'YYYY-MM-DD',
        istime: false,
        isclear: false, //是否显示清空
        istoday: false, //是否显示今天
        festival: true, //显示节日
        choose: function (datas) { //选择日期完毕的回调
            getCardRecords();
        }
    });
});

function init() {
    $.ajax({
        url: "/huaqiao/StudentFront/getCurStu?" + new Date().getTime(),
        async: false,
        success: function (str) {
            if (checkReturn(str)) {
                $("#stu_name").text(str.name);
                $("#class_name").text(str.classname);
            }
        }
    });
    $("a[href='#out']").click(function () {
        type = 0;
        pageStart = 1;
        getCardRecords();
    });
    // $("#date-out-start").focusout(function () {
    //     type = 0;
    //     pageStart = 1;
    //     getCardRecords();
    // });
    // $("#date-out-end").focusout(function () {
    //     type = 0;
    //     pageStart = 1;
    //     getCardRecords();
    // });
    $("a[href='#in']").click(function () {
        type = 1;
        pageStart = 1;
        getCardRecords();
    });
    // $("#date-in-start").focusout(function () {
    //     type = 1;
    //     pageStart = 1;
    //     getCardRecords();
    // });
    // $("#date-in-end").focusout(function () {
    //     type = 1;
    //     pageStart = 1;
    //     getCardRecords();
    // });
    $("a[href='#unusual']").click(function () {
        type = 2;
        pageStart = 1;
        getCardRecords();
    });
    // $("#date-unusual-start").focusout(function () {
    //     type = 2;
    //     pageStart = 1;
    //     getCardRecords();
    // });
    // $("#date-unusual-end").focusout(function () {
    //     type = 2;
    //     pageStart = 1;
    //     getCardRecords();
    // });
    getCardRecords();
}

function getCardRecords() {
    var sdate = null;
    var edate = null;
    if (type == 0) {
        sdate = $("#date-out-start").val();
        edate = $("#date-out-end").val();
    } else if (type == 1) {
        sdate = $("#date-in-start").val();
        edate = $("#date-in-end").val();
    } else {
        sdate = $("#date-unusual-start").val();
        edate = $("#date-unusual-end").val();
    }
    if (sdate == "") {
        sdate = "1970-01-01";
    }
    if (edate == "") {
        edate = "2160-01-01";
    }
    var startDate = new Date(sdate);
    var endDate = new Date(edate);
    endDate.setDate(endDate.getDate() + 1);
    $.ajax({
        url: "/huaqiao/StudentFront/getCardRecords?" + new Date().getTime(),
        type: "post",
        data: {
            startDate: DateUtils.format(startDate, "yyyy-MM-dd HH:mm:ss"),
            endDate: DateUtils.format(endDate, "yyyy-MM-dd HH:mm:ss"),
            pageSize: pageSize,
            pageStart: pageStart,
            type: type
        },
        async: false,
        success: function (str) {
            var table = null;
            switch (type) {
                case 0:
                    table = $("#out table");
                    break;
                case 1:
                    table = $("#in table");
                    break;
                case 2:
                    table = $("#unusual table");
                    break;
            }
            table.empty();
            if (checkReturn(str)) {
                if (str.length == 0) {
                    layer.msg("没有数据", {time: 1000});
                }

                for (var i = 0; i < str.length; i++) {
                    if (type != 2) {
                        table.append("<tr><td>" + str[i].studentName + "</td><td>" + DateUtils.format(new Date(str[i].createTime), 'yyyy-MM-dd HH:mm:ss') + "</td></tr>");
                    } else {
                        table.append("<tr><td>" + str[i].studentName + "</td><td>" + (str[i].inoutType == 1 ? '出校' : '进校') + "</td><td>" + DateUtils.format(new Date(str[i].createTime), 'yyyy-MM-dd HH:mm:ss') + "</td></tr>");
                    }
                }
            }
        }
    });
    $.ajax({
        url: "/huaqiao/StudentFront/getCardRecords?" + new Date().getTime(),
        type: "post",
        data: {
            startDate: DateUtils.format(startDate, "yyyy-MM-dd HH:mm:ss"),
            endDate: DateUtils.format(endDate, "yyyy-MM-dd HH:mm:ss"),
            type: type
        },
        async: false,
        success: function (str) {
            if (str != 0) {
                pageCount = Math.floor((str + pageSize - 1) / pageSize);
                setTableBTN();
            } else {
                layer.msg("没有数据", {time: 1000});
            }
        }
    });
}

function checkReturn(str) {
    if (str != null && str != "") {
        if (str.errorcode != undefined) {
            if (str.errorcode == 1) {
                if (islogin == 1) {
                    layer.msg(str.errormessage, {time: 1000});
                    window.location.href = "login_pc_student.html";
                    islogin = 0;
                }
            }
            return false;
        } else {
            return true;
        }
    } else {
        return false;
    }
}

function setTableBTN() {
    var pagination_li = null;
    var pagination_a = null;
    switch (type) {
        case 0:
            pagination_li = $("#out .pagination li");
            pagination_a = $("#out .pagination a");
            break;
        case 1:
            pagination_li = $("#in .pagination li");
            pagination_a = $("#in .pagination a");
            break;
        case 2:
            pagination_li = $("#unusual .pagination li");
            pagination_a = $("#unusual .pagination a");
            break;
    }
    pagination_li.each(function (i) {
        if (i == 1) {//上一页按钮
            this.onclick = function () {
                if (pageStart > 1) {
                    pageStart--;

                    if (pageStart < parseInt(pagination_a.eq(2).html())) {
                        pagination_a.eq(6).html(pageStart + 4);
                        pagination_a.eq(5).html(pageStart + 3);
                        pagination_a.eq(4).html(pageStart + 2);
                        pagination_a.eq(3).html(pageStart + 1);
                        pagination_a.eq(2).html(pageStart);
                    }
                    pagination_li.each(function (i) {
                        $(this).removeClass("active");
                        if (pageStart == parseInt($(this).text())) {
                            $(this).addClass("active");
                        }
                    });
                    getCardRecords();
                } else {
                    layer.msg("当前是第一页", {time: 1000});
                }
            };
        } else if (i == 7) {//下一页按钮
            this.onclick = function () {
                if (pageStart < pageCount) {
                    pageStart++;

                    if (pageStart > parseInt(pagination_a.eq(6).html())) {
                        pagination_a.eq(6).html(pageStart);
                        pagination_a.eq(5).html(pageStart - 1);
                        pagination_a.eq(4).html(pageStart - 2);
                        pagination_a.eq(3).html(pageStart - 3);
                        pagination_a.eq(2).html(pageStart - 4);
                    }
                    pagination_li.each(function (i) {
                        $(this).removeClass("active");
                        if (pageStart == parseInt($(this).text())) {
                            $(this).addClass("active");
                        }
                    });
                    getCardRecords();
                } else {
                    layer.msg("当前是最后一页", {time: 1000});
                }
            };
        } else if (i == 0) {
            this.onclick = function () {
                pageStart = 1;

                if (pageStart < parseInt(pagination_a.eq(2).html())) {
                    pagination_a.eq(6).html(pageStart + 4);
                    pagination_a.eq(5).html(pageStart + 3);
                    pagination_a.eq(4).html(pageStart + 2);
                    pagination_a.eq(3).html(pageStart + 1);
                    pagination_a.eq(2).html(pageStart);
                }
                pagination_li.each(function (i) {
                    $(this).removeClass("active");
                    if (pageStart == parseInt($(this).text())) {
                        $(this).addClass("active");
                    }
                });
                getCardRecords();
            };
        } else if (i == 8) {
            this.onclick = function () {
                pageStart = pageCount;

                if (pageStart > parseInt(pagination_a.eq(6).html())) {
                    pagination_a.eq(6).html(pageStart);
                    pagination_a.eq(5).html(pageStart - 1);
                    pagination_a.eq(4).html(pageStart - 2);
                    pagination_a.eq(3).html(pageStart - 3);
                    pagination_a.eq(2).html(pageStart - 4);
                }
                pagination_li.each(function (i) {
                    $(this).removeClass("active");
                    if (pageStart == parseInt($(this).text())) {
                        $(this).addClass("active");
                    }
                });
                getCardRecords();
            };
        } else {//普通翻页按钮
            this.onclick = function () {
                var index = parseInt(this.innerText);
                if (index > pageCount) {
                    index = pageCount;
                }
                pageStart = index;
                pagination_li.each(function (i) {
                    $(this).removeClass("active");
                    if (pageStart == parseInt($(this).text())) {
                        $(this).addClass("active");
                    }
                });
                getCardRecords();
            }
        }
    });
}